/*
 * @Project Name: FwWebCore
 *
 * @File Name: UpdateServlet.java
 *
 * @Date: 2017-3-23
 *
 * @Copyright: 2017 Xi'an JieZi Software Technology Ltd.  All rights reserved.
 */

package com.jain.webcore.servlet;

import com.jain.webcore.database.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * The database query servlet.
 */
@WebServlet("/Update")
public class UpdateServlet extends BaseServlet {
    /**
     * Default constructor.
     */
    public UpdateServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/x-javascript");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        String sql = request.getParameter("sql");

        JdbcUtils jdbcUtils = new JdbcUtils();
        jdbcUtils.getConnection();

        boolean isSuccess = jdbcUtils.update(sql);
        jdbcUtils.releaseConnect();

        String status = "1";
        if (isSuccess) {
            status = "0";
        }

        String result = "{status: "  + status + "}";
        doResponse(response, result);
    }

}
